package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.stat.model.StatActivityModel;
import com.starbaba.stat.service.StatMallItemService;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.stat.Stat_Mall_Item_Project;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatMallItemAction extends BaseAction implements ModelDriven<Stat_Mall_Item_Project>{

	@SuppressWarnings("deprecation")
	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	private Stat_Mall_Item_Project model = null;
	
	private StatMallItemService service;
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
	
	@Autowired
	@Qualifier("statMallItemServiceImpl")
	public void setService(StatMallItemService service) {
		this.service = service;
	}

	public String show() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String prodSql = "SELECT * FROM comm_product_prdid p";
		String channelSql = "SELECT * FROM comm_product_channel c";
		try {
			List<CommProductPrdid> prodList = dao.list(prodSql, CommProductPrdid.class);
			List<CommProductChannel> channelList = dao.list(channelSql, CommProductChannel.class);
			paramsMap.put("prodList", prodList);
			paramsMap.put("channelList", channelList);
		} catch (AppException e) {
			LogService.error(e);
		}
		
		return ActionSupport.SUCCESS;
	}

	public void queryList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		try {
			List<Stat_Mall_Item_Project> list = new ArrayList<Stat_Mall_Item_Project>();
			list = service.queryList(start, length, pageResponse, paramsMap);

			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		String value = exportCVS(service.queryList(null,null,pageResponse,paramsMap));
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	
	public String exportCVS(List<Stat_Mall_Item_Project> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("时间,平台,产品,渠道,用户类型,页面,模块,位置,商品名称,商品ID,商品pv,商品uv\r\n");
		for (Stat_Mall_Item_Project vo : list) {
			builder.append(vo.getTime()).append(","); 
			builder.append(vo.getPlatform()).append(",");
			builder.append(vo.getProdName()).append(",");
			builder.append(vo.getChannelName()).append(",");
			builder.append(vo.getUser_type()).append(",");
			builder.append(vo.getPage()).append(",");
			builder.append(vo.getModule()).append(",");
			builder.append(vo.getPosition()).append(",");
			builder.append(vo.getItem_name()).append(",");
			builder.append(vo.getItem_id()).append(",");
			builder.append(vo.getItem_pv()).append(",");
			builder.append(vo.getItem_uv()).append(",");
			builder.append("\r\n");
		}
		return builder.toString();
	}
	

	@Override
	public Stat_Mall_Item_Project getModel() {
		if (model == null) {
			model = new Stat_Mall_Item_Project();
		}
		return model;
	}

}
